package com.hr.builder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.Element;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.Model;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.TabbedSection;
import com.hr.SchedualDefine;
import com.hr.SchedualEmployeeScope;
import com.hr.SchedualScope;


@Component
public class SchedualDefineBuilder extends ModelBuilderSupport {
	static final String DEPTIDS = "deptIds";
	static final String DEPARTMENT = "department";
	static final String WEEKNBRS = "weekNbrs";
	static final String SCOPE = "scope";
	
	
	@Override
	public boolean init() {
		return super.setClass(SchedualDefine.class, "Schedual Define", "排班预设");
	}

	@Override
	public void buildFields() {
		super.buildFields();
		addModelField(DEPARTMENT, "Department", "部门");
		addModelField(DEPTIDS, "Department", "部门");
		addModelField(WEEKNBRS, "Week", "上班时间");
		addModelField(SCOPE, "Default Scope", "默认时间");
	}
	
	@Override
	protected void addSearchFields() {
		super.addSearchField(convertPropertyToField(DEPTIDS));
	}
	
	@Override
	protected void addListFields() {
		addListField(convertPropertyToField(DEPARTMENT, WEEKNBRS));
	}
	
	@Override
	protected void addDetailFields() {
		addDetailField(convertPropertyToField(DEPTIDS, WEEKNBRS, SCOPE));
	}
	
	@Override
	protected void gatherExproperties() {
		exProperties.put(REQUIRED, new String[]{DEPTIDS});
	}
	
	
	@Override
	protected Element convertFieldToElement(ModelField mf, boolean isListLayout) {
		Element element = super.convertFieldToElement(mf, isListLayout);
		String name = element.getName();
		if (!isListLayout){
			if (name.equals(WEEKNBRS)){
				Map<String, String> exDataTypeProperties = new HashMap<String, String>();
				exDataTypeProperties.put("collection", "fn.service('hr.schedual').srcWeekValues");
				element.setExProperty(exDataTypeProperties);
				element.setTagName("checkbox");
				element.addExProperty("width", "500");
			}	
		}
		
		return element;
	}
	
	@Override
	protected void afterBuilderLayout() {
		DetailLayout layout = getDefaultDetailLayout();

		FlatSection fsResult =  layout.createFlatSection();
		fsResult.setNamePrefix("model");
		
		Model schedualItemView = getModel(SchedualScope.class);
		
		TabbedSection ts = elementService.createTabbedSection(getDefaultDetailLayout(), 2);
		List<FlatSection> tabPages = ts.getTabPages();
		
		List<Element> schedualScopeElements = elementService.createSectionElement(tabPages.get(0), schedualItemView.getDefaultListLayout().getSortChildren());
		GridSection gridSection = getMainGridSection(schedualScopeElements);
		gridSection.setReadOnly(false);
		gridSection.setTagName("datagrid");
		gridSection.setNamePrefix("schedualScopes");
		
		tabPages.get(0).setI18n("默认排班");
		
		Model schedualEmployeeScopeView = getModel(SchedualEmployeeScope.class);
		List<Element> schedualEmployeeScopeElements = elementService.createSectionElement(tabPages.get(1), schedualEmployeeScopeView.getDefaultListLayout().getSortChildren());
		GridSection employeeScope = getMainGridSection(schedualEmployeeScopeElements);
		employeeScope.setReadOnly(false);
		employeeScope.setTagName("datagrid");
		employeeScope.setNamePrefix("employeeScopes");
		
		tabPages.get(1).setI18n("默认员工排班");
	}
	
}
